<template>
  <div>
    <el-dialog title="新增成员" :visible.sync="dialog" direction="rtl" ref="drawer" width="25%" height="25%"
      :close-on-click-modal="false">
      <el-form ref="empForm" :model="empForm" :rules="rules" label-width="80px">
        <el-form-item label="姓名" prop="nickname">
          <el-input v-model="empForm.nickname"></el-input>
        </el-form-item>
        <el-form-item label="	手机号" prop="cellphone">
          <el-input v-model="empForm.cellphone"></el-input>
        </el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input v-model="empForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input type='password' v-model="empForm.password"></el-input>
        </el-form-item>
        <el-form-item label="角色" prop="roleId">
          <el-select @change="selectChanged" v-model="empForm.roleId" placeholder="请选择角色" style="width: 100%;">
            <el-option v-for="item in roleList" :key="item.roleId" :label="item.roleName" :value="item.roleId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="门店">
          <el-select @change="selectShopChanged" v-model="empForm.advertisingId" placeholder="请选择门店" style="width: 100%;">
            <el-option v-for="item in shopList" :key="item.advertisingId" :label="item.advertisingName" :value="item.advertisingId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm">确认</el-button>
          <el-button @click="dialog = false">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
  import {
    validateCellphone
  } from '@/utils/validate'
  import {
    getRoleList
  } from '@/api/role.js'
  import {
    empAddUser,
    empInfo,
    empUpdate,
    listAdvertising
  } from '@/api/emp.js'
  import {
    md5
  } from '@/utils/MD5.js'

  export default {
    data: function() {
      const validCellphone = (rule, value, callback) => {
        if (!validateCellphone(value)) {
          callback(new Error('请输入正确的手机号吗'))
        } else {
          callback()
        }
      }
      return {
        dialog: false,
        roleList: [],
        shopList: [{
          advertisingId:0,
          advertisingName:'无',
        }],
        userId:'',
        empForm: {
          nickname: '',
          cellphone: '',
          username: '',
          password: '',
          roleId: '',
          advertisingId:0,
        },
        rules: {
          nickname: [{
            required: true,
            message: '请输入姓名',
            trigger: 'blur'
          }],
          cellphone: [{
            required: true,
            trigger: 'blur',
            validator: validCellphone
          }],
          username: [{
            required: true,
            message: '请输入账号',
            trigger: 'blur'
          }],
          password: [{
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }],
          roleId: [{
            required: true,
            message: '请选择角色',
            type:'number',
            trigger:['blur','change']
          }]
        }
      }
    },
    methods: {

      async empUpdate() {
        var params = {
          userId: this.userId,
          roleId: this.empForm.roleId,
          nickname: this.empForm.nickname,
          cellphone: this.empForm.cellphone,
          username: this.empForm.username,
          password: md5(this.empForm.password),
          advertisingId: this.empForm.advertisingId
        };
        var res = await empUpdate(params)
        this.close()
      },

      async loadRoleList() {
        var res = await getRoleList();
        this.roleList = res.data;
      },

      async listAdvertising() {
        var res = await listAdvertising();
        this.shopList = res.data;
      },

      async loadEmpInfo(userId) {
        var res = await empInfo(userId);
        this.empForm.nickname = res.data.nickname;
        this.empForm.cellphone = res.data.cellphone;
        this.empForm.username = res.data.username;
        this.empForm.password = res.data.password;
        this.empForm.roleId = res.data.roleId;
        this.empForm.advertisingId = res.data.advertisingId;
      },

      open(userId) {
        this.dialog = true;
        this.loadRoleList();
        this.listAdvertising();
        this.loadEmpInfo(userId);
        this.userId = userId;
      },

      close() {
        this.dialog = false;
        this.$parent.empList();
      },
      selectChanged(value) {
        this.empForm.roleId = value;
      },
      selectShopChanged(value){
        this.empForm.advertisingId = value;
      },

      submitForm() {
        this.$refs.empForm.validate(valid => {
          if (!valid) return
          this.empUpdate();
        })
      },

      resetForm() {
        if (this.$refs.empForm !== undefined) {
          this.$refs.empForm.resetFields();
        }
      },
    }
  }
</script>

<style scoped lang="scss">
  .QRCodeDownload {
    padding: 15px;
  }

  // 列表字段居中
  /deep/.el-table th>.cell {
    text-align: center;
  }

  /deep/.el-table .cell {
    text-align: center;
  }
</style>
